package com.cloudfast.dingtalk;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.aliyun.dingtalkcontact_1_0.models.GetUserHeaders;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.dingtalk.api.response.OapiV2UserGetResponse.UserGetResponse;

/**
 * 回调; 第三方授权;本接口值允许钉钉回调
 *
 * @author liuyw
 * @date 2022年9月11日
 */
@RestController
public class DingTalkLogin {

    public static com.aliyun.dingtalkoauth2_1_0.Client authClient() throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkoauth2_1_0.Client(config);
    }

    /**
     * 获取用户token
     *
     * @param authCode
     * @return
     * @throws Exception
     */
    // 接口地址：注意/auth与钉钉登录与分享的回调域名地址一致
    @GetMapping(value = "/cloud/oauth/third/auth")
    public String getAccessToken(@RequestParam(value = "authCode") String authCode) throws Exception {
        com.aliyun.dingtalkoauth2_1_0.Client client = authClient();
        GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest()

                // 应用基础信息-应用信息的AppKey,请务必替换为开发的应用AppKey
                .setClientId("dingwxxxxx请替换为正确的应用信息的AppKey")

                // 应用基础信息-应用信息的AppSecret，,请务必替换为开发的应用AppSecret
                .setClientSecret("ICLbFFjNxxx请替换为正确的应用信息的AppSecret").setCode(authCode)
                .setGrantType("authorization_code");
        GetUserTokenResponse getUserTokenResponse = client.getUserToken(getUserTokenRequest);
        // 获取用户个人token
        String accessToken = getUserTokenResponse.getBody().getAccessToken();
        return getUserinfo(accessToken);

    }

    /**
     * 钉钉授权(免密)登入(钉钉登入与分享设置)
     *
     * @param authCode
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/cloud/oauth/auth")
    public UserGetResponse dingAuthLOgin(@RequestParam(value = "authCode") String authCode) throws Exception {
        CloudDingTalk cloudDingTalk = new CloudDingTalk();
        String accessToken = cloudDingTalk.thirdToken("", "", "", "");
        // 通过临时授权码获取授权用户的个人信息
        String unionid = cloudDingTalk.getUnionid(authCode, authCode, authCode);
        // 通过 unionid 获取userid
        String userId = cloudDingTalk.getUserId(unionid, accessToken);

        return cloudDingTalk.getUserGetResponse(userId, accessToken);
    }

    public static com.aliyun.dingtalkcontact_1_0.Client contactClient() throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkcontact_1_0.Client(config);
    }

    /**
     * 获取用户个人信息
     *
     * @param accessToken
     * @return
     * @throws Exception
     */
    public String getUserinfo(String accessToken) throws Exception {
        com.aliyun.dingtalkcontact_1_0.Client client = contactClient();
        GetUserHeaders getUserHeaders = new GetUserHeaders();
        getUserHeaders.xAcsDingtalkAccessToken = accessToken;
        // 获取用户个人信息，如需获取当前授权人的信息，unionId参数必须传me
        String me = JSON.toJSONString(client.getUserWithOptions("me", getUserHeaders, new RuntimeOptions()).getBody());
        return me;
    }
}